package goldeneagle.com.admin.servlet;

import goldeneagle.com.admin.service.AdminManagerService;
import goldeneagle.com.entity.Product;
import goldeneagle.com.util.Page;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/ProductList")
public class ShowProductPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number = request.getParameter("pageNum");
        System.out.println(number);
        int pageNum;
        if(number == null || "".equals(number)){
            pageNum = 1;
        } else {
            pageNum = Integer.parseInt(number);
        }
        //AdminManagerDaoImpl adminManagerDao = new AdminManagerDaoImpl();
        AdminManagerService adminManagerService = new AdminManagerService();
        List<Product> page = adminManagerService.adminSelectProductList(pageNum,2);
       // List<Product> page = adminManagerDao.adminSelectProductList(1,2);
        //page.forEach(action-> System.out.println(action.getName()));
        Integer count = (int) adminManagerService.totalCount("Product");
        System.out.println("商品个数"+count);
        Page<Product> p = new Page<>(pageNum, 2);
        p.setTotalCount(count);
        p.setList(page);
        //System.out.println(p.getList().toString());
        //response.getWriter().write(JSONObject.toJSONString(p.getList()));
        request.setAttribute("Product",p);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}
